package com.cnswhy.cloud.server.merchant.controller.iot;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.mq.CreateTerminalMQ;
import com.cnswhy.cloud.server.core.dto.IotTerminalApplyInfoPageDTO;
import com.cnswhy.cloud.server.core.dto.IotTerminalApplyInfoSaveDTO;
import com.cnswhy.cloud.server.core.dto.IotTerminalApplyInfoUpdateDTO;
import com.cnswhy.cloud.server.core.entity.IotTerminalApplyInfo;
import com.cnswhy.cloud.server.core.service.IotTerminalApplyInfoService;
import com.cnswhy.cloud.server.merchant.dto.IotTerminalApplyInfoVerifyDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2021-02-18
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/iotTerminalApplyInfo")
@Api(value = "IotTerminalApplyInfo", tags = "")
//@PreAuth(replace = "iotTerminalApplyInfo:")
public class IotTerminalApplyInfoController extends SuperController<IotTerminalApplyInfoService, Long, IotTerminalApplyInfo, IotTerminalApplyInfoPageDTO, IotTerminalApplyInfoSaveDTO, IotTerminalApplyInfoUpdateDTO> {
    @Autowired
    IdGeneratorService idGeneratorService;
    @Autowired
    MessageService messageService;
    @Override
    public R<IotTerminalApplyInfo> handlerSave(IotTerminalApplyInfoSaveDTO model) {
        model.setBatchOrderId(model.getIotType().getCode() + idGeneratorService.genCommonIdString());
        model.setStatus(0);
        model.setStartIndex(0L);
        model.setEndIndex(model.getApplySum());
        return R.successDef();
    }
    @ApiOperation(value = "审核")
    @PostMapping("/verifyStatus")
    public R<Boolean> verifyStatus(@RequestBody IotTerminalApplyInfoVerifyDTO iotTerminalApplyInfoVerifyDTO) {
        log.info(JSON.toJSONString(iotTerminalApplyInfoVerifyDTO));
        List<IotTerminalApplyInfo> iotTerminalApplyInfos = baseService.listByIds(iotTerminalApplyInfoVerifyDTO.getIds());
        for(IotTerminalApplyInfo iotTerminalApplyInfo : iotTerminalApplyInfos){
            iotTerminalApplyInfo.setStatus(iotTerminalApplyInfoVerifyDTO.getVerifyStatus());
            baseService.updateById(iotTerminalApplyInfo);
            if(iotTerminalApplyInfo.getStatus() == 1){
                messageService.sendQueueByObject(AmqpExchange.ADD_TERMINAL_MESSAGE, CreateTerminalMQ.builder().iotTerminalApplyInfoId(iotTerminalApplyInfo.getId()).build());
            }
        }
        return success();
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<IotTerminalApplyInfo> iotTerminalApplyInfoList = list.stream().map((map) -> {
            IotTerminalApplyInfo iotTerminalApplyInfo = IotTerminalApplyInfo.builder().build();
            return iotTerminalApplyInfo;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(iotTerminalApplyInfoList));
    }
}
